package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUserWalletCode;
import com.ksd.modules.members.mapper.AdminUserWalletCodeMapper;
import com.ksd.modules.members.po.AdminUserWalletCodeSavePO;
import com.ksd.modules.members.po.AdminUserWalletCodeSearchPO;
import com.ksd.modules.members.service.IAdminUserWalletCodeService;
import com.ksd.modules.members.vo.AdminUserWalletCodeVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminUserWalletCodeServiceImpl extends BaseServiceImpl<AdminUserWalletCodeMapper, AdminUserWalletCode> implements IAdminUserWalletCodeService {

    /**
     * 查询列表
     * @param adminUserWalletCodeSearchPO
     * @return
     */
    public Page<AdminUserWalletCodeVo> findAdminUserWalletCodes(AdminUserWalletCodeSearchPO adminUserWalletCodeSearchPO){
        Page<AdminUserWalletCode> page = new Page<>(adminUserWalletCodeSearchPO.getPageNo(),adminUserWalletCodeSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserWalletCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserWalletCode::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletCodeSearchPO.getCode()), AdminUserWalletCode::getCode,adminUserWalletCodeSearchPO.getCode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserWalletCodeSearchPO.getMark()), AdminUserWalletCode::getMark,adminUserWalletCodeSearchPO.getMark());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserWalletCodeSearchPO.getCoin()), AdminUserWalletCode::getCoin,adminUserWalletCodeSearchPO.getCoin());
        lambdaQueryWrapper.orderByDesc(AdminUserWalletCode::getCreateTime);
        Page<AdminUserWalletCode> adminUserWalletCodePage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserWalletCodeVo> adminUserWalletCodeVoPage = tranferPageBo(adminUserWalletCodePage, AdminUserWalletCodeVo.class);
        return adminUserWalletCodeVoPage;
    }


    /**
     * 创建兑换码
     * @param adminUserWalletCodeSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean createBatchCode(AdminUserWalletCodeSavePO adminUserWalletCodeSavePO){
        AdminUserWalletCode adminUserWalletCode= tranferBo(adminUserWalletCodeSavePO, AdminUserWalletCode.class);
        adminUserWalletCode.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWalletCode.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserWalletCode.setCode(new SnowflakeIdWorker(9,8).nextId()+"");
        adminUserWalletCode.setMark(0);
        this.saveOrUpdate(adminUserWalletCode);
        return true;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserWalletCodeVo getAdminUserWalletCodeById(Long id){
        AdminUserWalletCode adminUserWalletCode = this.getById(id);
        AdminUserWalletCodeVo adminUserWalletCodeVo = tranferBo(adminUserWalletCode, AdminUserWalletCodeVo.class);
        return adminUserWalletCodeVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserWalletCodeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserWalletCode(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


}
